/*
 * 文件名：NewsController.java
 * 版权：Copyright 2007-2016 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述： NewsController.java
 * 修改人：yanshu
 * 修改时间：2016年1月26日
 * 修改内容：新增
 */
package com.feichuang.chineseWoodCarving.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.better517na.commons.Response;
import com.better517na.commons.StatusCode;
import com.feichuang.chineseWoodCarving.business.INewsInfoBussiness;
import com.feichuang.chineseWoodCarving.model.MNewsInfo;
import com.feichuang.chineseWoodCarving.model.MPagerRequestVo;
import com.feichuang.chineseWoodCarving.model.MPagerResultVo;
import com.google.gson.Gson;

/**
 * TODO 新闻资讯/根雕知识.
 * <p>
 * TODO 详细描述
 * <p>
 * TODO 示例代码
 * 
 * <pre>
 * </pre>
 * 
 * @author yanshu
 */
@Controller
@RequestMapping(value = "/NewsCon")
public class NewsController {
    /**
     * @
     */
    @Resource(name = "newsInfoBussiness")
    private INewsInfoBussiness newsInfoBussiness;

    /**
     * 
     * TODO 首页.
     * 
     * @param request
     *            请求
     * @param response
     *            响应
     * @param param
     *            请求json
     * @throws IOException
     */
    @RequestMapping(value = "/pageQuery", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectNews(HttpServletRequest request, HttpServletResponse response, String param) throws IOException {
        Gson gson = new Gson();
        Response<MPagerResultVo<?>> res = new Response<MPagerResultVo<?>>();
        try {
            MPagerRequestVo<MNewsInfo> pageRequest = gson.fromJson(param, MPagerRequestVo.class);
            MPagerResultVo<MNewsInfo> pageResult = newsInfoBussiness.selectMNewsInfoForPage(pageRequest);
            res.setContent(pageResult);
            res.setCode(StatusCode.SUCCESS);
            res.setMessage("成功");
        } catch (Exception e) {
            res.equals(StatusCode.ERROR);
            res.setMessage("风速过大");
        }
        return gson.toJson(res);
    }

    /**
     * 
     * TODO 获取json给前端同学使用.
     *
     */
    @Test
    public void getJson() {
        Gson gson = new Gson();
        Response<MPagerResultVo<MNewsInfo>> res = new Response<MPagerResultVo<MNewsInfo>>();
        List<MNewsInfo> list = new ArrayList<MNewsInfo>();
        // 消息内容填充
        MNewsInfo newsInfo = new MNewsInfo();
        newsInfo.setAddTime(new Date());
        newsInfo.setIsDelete(0);
        newsInfo.setKeyId("主键");
        newsInfo.setTitle("标题");
        newsInfo.setType(1);
        newsInfo.setUrl("页面地址");
        list.add(newsInfo);
        // 分页信息封装
        MPagerResultVo<MNewsInfo> pageInfo = new MPagerResultVo<MNewsInfo>();
        pageInfo.setDataInfo(list);
        pageInfo.setTotalCount(1);
        // 返回值封装
        res.setContent(pageInfo);
        res.setCode(1);
        // 请求参数构造
        MPagerRequestVo<MNewsInfo> pageRequest = new MPagerRequestVo<MNewsInfo>();
        pageRequest.setDataModel(newsInfo);
        pageRequest.setCurrentpage(1);
        pageRequest.setSize(10);
        // 请求消息序列化
        String jsonRequest = gson.toJson(pageRequest);
        System.out.println(jsonRequest);
        // 响应消息序列化
        String jsonResponse = gson.toJson(res);
        System.out.println(jsonResponse);
    }

}
